import _objectSpread from "@babel/runtime/helpers/esm/objectSpread2";
import _regeneratorRuntime from "@babel/runtime/regenerator";

/**
 * model 地区定义
 * @date: 2018-6-19
 */
import { getResponse, createPagination, parseParameters } from 'utils/utils';
import { fetchCountryList as _fetchCountryList, createCountry as _createCountry, updateCountry as _updateCountry } from "../services/countryService";
export default {
  namespace: 'country',
  state: {
    modalVisible: false,
    countryList: [],
    pagination: {} // 分页对象

  },
  effects: {
    // 获取国家信息
    fetchCountryList: /*#__PURE__*/_regeneratorRuntime.mark(function fetchCountryList(_ref, _ref2) {
      var payload, call, put, res, list;
      return _regeneratorRuntime.wrap(function fetchCountryList$(_context) {
        while (1) {
          switch (_context.prev = _context.next) {
            case 0:
              payload = _ref.payload;
              call = _ref2.call, put = _ref2.put;
              _context.next = 4;
              return call(_fetchCountryList, parseParameters(payload));

            case 4:
              res = _context.sent;
              list = getResponse(res);

              if (!list) {
                _context.next = 9;
                break;
              }

              _context.next = 9;
              return put({
                type: 'updateState',
                payload: {
                  countryList: list.content,
                  pagination: createPagination(list)
                }
              });

            case 9:
              return _context.abrupt("return", list);

            case 10:
            case "end":
              return _context.stop();
          }
        }
      }, fetchCountryList);
    }),
    // 新增国家
    createCountry: /*#__PURE__*/_regeneratorRuntime.mark(function createCountry(_ref3, _ref4) {
      var payload, call, param, res;
      return _regeneratorRuntime.wrap(function createCountry$(_context2) {
        while (1) {
          switch (_context2.prev = _context2.next) {
            case 0:
              payload = _ref3.payload;
              call = _ref4.call;
              param = payload;
              param.enabledFlag = payload.enabledFlag ? 1 : 0;
              _context2.next = 6;
              return call(_createCountry, param);

            case 6:
              res = _context2.sent;
              return _context2.abrupt("return", getResponse(res));

            case 8:
            case "end":
              return _context2.stop();
          }
        }
      }, createCountry);
    }),
    // 更新国家
    updateCountry: /*#__PURE__*/_regeneratorRuntime.mark(function updateCountry(_ref5, _ref6) {
      var payload, call, param, res;
      return _regeneratorRuntime.wrap(function updateCountry$(_context3) {
        while (1) {
          switch (_context3.prev = _context3.next) {
            case 0:
              payload = _ref5.payload;
              call = _ref6.call;
              param = payload;
              param.enabledFlag = payload.enabledFlag ? 1 : 0;
              _context3.next = 6;
              return call(_updateCountry, param);

            case 6:
              res = _context3.sent;
              return _context3.abrupt("return", getResponse(res));

            case 8:
            case "end":
              return _context3.stop();
          }
        }
      }, updateCountry);
    })
  },
  reducers: {
    updateState: function updateState(state, _ref7) {
      var payload = _ref7.payload;
      return _objectSpread(_objectSpread({}, state), payload);
    }
  }
};